//初始化页面 js
define(function (require, exports, module) {
    const Vue=require("vue");
    var template=require('text!views/app.html');
    const api=require("api");
    const sdk=require("sdk");
    const utils=require('utils');
    module.exports=Vue.extend({
        template:template,
        data:function () {
            return {
            }
        },
        created:function () {
            if(!this.oauth.open_id||this.oauth.open_id==="undefined"){
                if(utils.isWeixn()) {
                    var params = utils.param2Obj(window.location.search);
                    if (params.code && params.state) {
                        ////发送code换取用户信息
                        this.$store.dispatch("oauthInfo", params);//oauthUserInfo
                        //jssdk初始化
                        this.$store.dispatch('SdkInit');
                    } else {
                        window.location.href = utils.oauthUrl();
                    }
                }
            }
            else {
                if(parseInt(this.oauth.subscribe)===1){
                    //jssdk初始化
                    this.$store.dispatch('SdkInit');
                }else {
                    //获取用户是否关注
                    this.$store.dispatch("oauthInfo",{openid:this.oauth.open_id}).then(function (res) {
                        if(parseInt(res.subscribe)===1){
                            //jssdk初始化
                            this.$store.dispatch('SdkInit');
                        }else {
                            this.$router.push({path:'/code'});
                        }
                    });
                }
            }
            //更新用户数据
            if(this.user_info.mobile_phone){
                this.$store.dispatch('UserInfo',this.user_info);
            }else{
                this.$router.push({path: '/phone'});
            }
        },
        computed:{
            oauth:function () {
                return this.$store.getters.getOAuth;
            },
            user_info:function () {
                return this.$store.getters.getUserInfo;
            },
            history:function () {
                return this.$store.getters.getHistory;
            }
        },
        mounted:function(){

        },
        watch:{
            //监听路由变化
            '$route':function (to,from) {
                from.meta.scrollTop=window.document.body.scrollTop;
                if(this.history.length>0){
                    var lastHistory=this.history[this.history.length-1];
                    //回退
                    if(lastHistory.from.path===to.path){
                        this.$store.commit("backHistory");
                    }
                    //前进
                    else{
                        this.$store.commit("addHistory",{to:to,from:from});
                    }
                }else{
                    this.$store.commit("addHistory",{to:to,from:from});
                }
                if(to.meta&&to.meta.auth){
                    if(!this.user_info.mobile_phone&&this.$route.path!=="/phone"){
                        this.$router.push({path: '/phone'});
                    }
                }
            }
        },
        method:{

        }
    });
});